import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:free_music_player/providers/music_provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:free_music_player/widgets/playing_screen.dart';

class PlayerBottomBar extends StatelessWidget {
  const PlayerBottomBar({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<MusicProvider>(
      builder: (context, provider, child) {
        if (provider.currentSong == null) {
          return const SizedBox.shrink();
        }

        final song = provider.currentSong!;

        return Container(
          height: 60,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                blurRadius: 10,
                offset: const Offset(0, -5),
              ),
            ],
          ),
          child: InkWell(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const PlayingScreen(),
                ),
              );
            },
            child: Row(
              children: [
                // 专辑封面
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: CachedNetworkImage(
                    imageUrl: song.album.picUrl,
                    width: 44,
                    height: 44,
                    fit: BoxFit.cover,
                    placeholder: (context, url) => const
                        CircularProgressIndicator(),
                    errorWidget: (context, url, error) =>
                        const Icon(Icons.music_note),
                  ),
                ),

                // 歌曲信息
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        song.name,
                        style: const TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      Text(
                        song.artistsName,
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey[600],
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ),

                // 播放控制
                Row(
                  children: [
                    IconButton(
                      icon: const Icon(Icons.skip_previous),
                      onPressed: () {
                        provider.previousSong();
                      },
                    ),
                    IconButton(
                      icon: Icon(
                        provider.isPlaying ? Icons.pause : Icons.play_arrow,
                        size: 24,
                      ),
                      onPressed: () {
                        if (provider.isPlaying) {
                          provider.pause();
                        } else {
                          provider.resume();
                        }
                      },
                    ),
                    IconButton(
                      icon: const Icon(Icons.skip_next),
                      onPressed: () {
                        provider.nextSong();
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}